/*
 * @(#)SaveXmlAction.java
 *
 *
 * ver 1.0 Oct 3, 2007 ggranum
 */
package org.testfire.agent.action;

import java.io.File;
import java.util.Map;
import org.testfire.agent.AgentProperties;
import org.testfire.agent.runner.TestSetRunner;
import org.testfire.agent.util.TestsetUtil;
import org.testfire.model.TestsetType;
import org.testfire.support.S;

// eof: SaveXmlAction.java

/**
 * Save an XML Testset to the local store.
 */
public class RunAction extends Action
{
    public RunAction()
    {
        super( false, new String[]{"userName"}, new String[]{"testset"} );
    }

    public String perform( Map<String, String> params, Map<String, String[]> paramValues )
    {

        String userName = params.get( "userName" );
        String[] sets = paramValues.get( "testset" );
        String xml = params.get( "testset_xml" );
        if ( !S.empty( xml ) )
        {
            perform( xml );
        }
        File userDir = new File( AgentProperties.getInstance().getSaveDirectory(), userName );

        for ( String s : sets )
        {
            int idx = s.lastIndexOf( '.', s.lastIndexOf( '.' ) - 1 );

            String name = s.substring( idx + 1 );
            String packPath = s.substring( 0, idx );
            packPath = packPath.replace( '.', '/' );

            File dir = new File( userDir, packPath );
            File f = new File( dir, name );

            if ( f.exists() )
            {
                this.perform( f );
            }
        }
        return "";
    }

    public void perform( File file )
    {
        TestsetType testset = TestSetRunner.run( TestsetUtil.getTestSet( file ) );
        new SaveAction().perform( testset );
    }

    public void perform( String xml )
    {
        TestsetType testset = TestSetRunner.run( TestsetUtil.getTestSet( xml ) );
        new SaveAction().perform( testset );
        
    }


}